package com.limao.CacheManager;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.CrowdSysProjectClass;
import com.limao.DBProvider.CrowdSysProjectProvider;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Tool.CacheClass;
import com.limao.cache.BasicModule;

public class CrowdSysProjectCache  extends BasicModule{

	private CrowdSysProjectProvider provider;
	private static CrowdSysProjectCache instance;
    
    public static CrowdSysProjectCache getInstance() {
        return instance;
    }

    private Cache<String, CrowdSysProjectClass> crowdSysProjectCache;


    public CrowdSysProjectCache() {
    	super("CrowdSysProjectCache");
    	crowdSysProjectCache = CacheFactory.createCache("crows_SysProjectCache");
    	
		initProvider();

    }
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
    
    public void ClearObjectCache(String id)
    {
    	CrowdSysProjectClass tmp = crowdSysProjectCache.get(id);
    	if(tmp!=null)
    	{
    		crowdSysProjectCache.remove(tmp.projectid);
    	}
    		

    } 
  
    //**************************************************************************


	//&新鲜宝  取某众筹详细信息（项目主页）
    public CrowdSysProjectClass load_Goods_Info_ById(int id)
    {
    	if(id<0) return null;
    	CrowdSysProjectClass tmp=crowdSysProjectCache.get(id+"");
    	if(tmp==null)
    	{
    		tmp=provider.load_Goods_Info_ById(id);
    	   	if(tmp!=null)
        	{
    	   		crowdSysProjectCache.put(id+"",tmp);
        	}
    	}
    	return tmp;
    	
    }
    
   
	/** 发布话题 或回复 **/
  	public boolean insertDiscuss_ByProjectid(int userid,int projectid,int replayid,String tex)
  	{
  		
  		return provider.insertDiscuss_ByProjectid(userid,projectid,replayid,tex);
  		
  	}
  //&新鲜宝  取某众筹详细信息（话题页）
	public PublicData load_Goods_Discuss_ById(int shopid,int curpage) throws LzPluginException
	{
		return provider.load_Goods_Discuss_ById(shopid,curpage);
	}
    
	
	//&新鲜宝  取某众筹详细信息（支持者）
	public PublicData load_Goods_Buy_ById(int shopid,int curpage) throws LzPluginException
	{
		return provider.load_Goods_Buy_ById(shopid,curpage);
	}
	    	
 
    //取众筹项目列表信息
	public PublicData getGoods_ListForCommon(int cateid,String keyword,int curpage)
	{
		return provider.getGoods_ListForCommon(cateid,keyword,curpage,0);
	} 
	public PublicData getGoods_ListForCommon(int cateid,String keyword,int curpage,int priceid)
	{
		return provider.getGoods_ListForCommon(cateid,keyword,curpage,priceid);
	} 
	
	//&新鲜宝 发表话题成功后，更新话题数，
    public void update_projectDisscussnum(int projectid)
    {
    	if(projectid<0) return ;
    	CrowdSysProjectClass obj=load_Goods_Info_ById(projectid);
    	if(obj==null)return;
    	obj.disscussnum=(CacheClass.getIntValue(obj.disscussnum)+1)+"";
    	crowdSysProjectCache.put(projectid+"", obj);
       	
    }

    
    //**************************************************************************
    private void initProvider() {
        if (provider == null ) {
            try {
                provider =CrowdSysProjectProvider.getInstance();
            }
            catch (Exception e) {
            	e.printStackTrace();
            }
        }
    }

}
